package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.ab.http.OnMessageResponse;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.CurriculumScheduleDetailedAdapter;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.base.JsonTools;
import com.baidumap.administrator.happystudy.base.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

public class CurriculumScheduleDetailedActivity extends AppBaseActivity implements OnMessageResponse {


    public ImageLoader loader;
    @Bind(R.id.goback)
    LinearLayout goback;
    private ListView mListView;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private CurriculumScheduleDetailedAdapter mAdapter;
    private static int pageNo = 1;
    private ArrayList<Map<String, Object>> arrayList;
    private String mTitle;

    @Override
    public int bindLayout() {
        return R.layout.activity_curriculum_schedule_detailed;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        getBundleValue();
        iniListView(view);
        Api.courseLists(this, sHelper.getString(Constant.STU_TOKEN), sHelper.getString(Constant.STU_UID));
    }

    private void getBundleValue() {
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {

            mTitle = bundle.getString("title");
        }
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(mTitle);
    }

    private void iniListView(View view) {

        // LoadImage初始化
        loader = ImageLoader.getInstance();
        pageNo = 1;
        mListView = (ListView) view.findViewById(R.id.list);
        mAdapter = new CurriculumScheduleDetailedAdapter(CurriculumScheduleDetailedActivity.this, loader);
        new MyTask().execute("", "" + pageNo);// 线程加载json数据
    }

    @OnClick(R.id.goback)
    public void onClick() {
        finish();
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }


    class MyTask extends
            AsyncTask<String, Void, ArrayList<Map<String, Object>>> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected ArrayList<Map<String, Object>> doInBackground(
                String... params) {

            ArrayList<Map<String, Object>> mList = null;
            mList = (ArrayList<Map<String, Object>>) JsonTools.listKeyMaps(
                    "list", Utils.HOUSE_JSON);
            return mList;
        }

        protected void onPostExecute(ArrayList<Map<String, Object>> result) {
            // TODO Auto-generated method stub

            list.addAll(result);//

            mAdapter.bindData(list);
            if (pageNo == 1) {

                mListView.setAdapter(mAdapter);
            }
            pageNo++;
            mAdapter.notifyDataSetChanged();
            super.onPostExecute(result);

        }
    }
}
